<?php
/**
 * Code: FN-0002
 * @author hnguyen
 */
include_once(PS_ADMIN_DIR . '/../classes/AdminTab.php');

class AdminCities extends AdminTab
{
    public function __construct()
    {
        global $cookie;
        if (!$id_lang = $cookie->id_lang)
            $id_lang = Configuration::get('PS_LANG_DEFAULT');

        $this->table = 'city';
        $this->className = 'City';
        $this->lang = true;
        $this->edit = true;
        $this->delete = true;
        $this->_select = 'col.name as country';
        $this->_join = 'LEFT JOIN `' . _DB_PREFIX_ . 'country` co ON (a.`id_country` = co.`id_country`)
						LEFT JOIN ' . _DB_PREFIX_ . 'country_lang col ON (col.`id_country` = a.`id_country`) AND (col.id_lang = ' . (int)$id_lang . ')';


        $this->fieldsDisplay = array(
            'id_city' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25),
            'name' => array('title' => $this->l('City'), 'width' => 130, 'filter_key' => 'b!name'),
            'country' => array('title' => $this->l('Country'), 'width' => 100, 'filter_key' => 'z!name'),
            'a!active' => array('title' => $this->l('Enabled'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false, 'filter_key' => 'a!active'));

        parent::__construct();
    }

    public function postProcess()
    {
        // TODO: do something here. Don't know yet.
        parent::postProcess();
    }


    public function displayForm($isMainTab = true)
    {
        global $currentIndex;
        global $cookie;

        parent::displayForm();

        if (!($obj = $this->loadObject(true))) {
            return;
        }

        echo '
		<form action="' . $currentIndex . '&submitAdd' . $this->table . '=1&token=' . $this->token . '" method="post">
		' . ($obj->id ? '<input type="hidden" name="id_' . $this->table . '" value="' . $obj->id . '" />' : '') . '
			<fieldset><legend><img src="../img/admin/world.gif" />' . $this->l('City') . '</legend>
				<label>' . $this->l('City:') . ' </label>
				<div class="margin-form">';

        foreach ($this->_languages as $language)
            echo '
					<div id="name_' . $language['id_lang'] . '" style="display: ' . ($language['id_lang'] == $this->_defaultFormLanguage ? 'block' : 'none') . '; float: left;">
						<input size="30" type="text" name="name_' . $language['id_lang'] . '" value="' . htmlentities($this->getFieldValue($obj, 'name', (int)($language['id_lang'])), ENT_COMPAT, 'UTF-8') . '" /><sup> *</sup>
						<span class="hint" name="help_box">' . $this->l('Invalid characters:') . ' <>;=#{}<span class="hint-pointer">&nbsp;</span></span>
					</div>';
        $this->displayFlags($this->_languages, $this->_defaultFormLanguage, 'name', 'name');
        echo '		<p style="clear: both">' . $this->l('Name of city') . '</p>
				</div>
				<label>' . $this->l('Country:') . ' </label>
				<div class="margin-form">
					<select name="id_country">';
        $countries = Country::getCountries($cookie->id_lang, true, false, false);
        foreach ($countries AS $country) {
            echo '		<option value="' . (int)($country['id_country']) . '"' . (($this->getFieldValue($obj, 'id_country') == $country['id_country']) ? ' selected="selected"' : '') . '>' . $country['name'] . '</option>';
        }
        echo '
                    </select>
                </div>
                <label>' . $this->l('Postal Code:') . ' </label>
				<div class="margin-form">
					<input type="text" size="4" maxlength="3" name="postal" value="' . htmlentities($this->getFieldValue($obj, 'postal'), ENT_COMPAT, 'UTF-8') . '"/> <sup>*</sup>
					<p>' . $this->l('Postal Code (2-4)') .'</p>
				</div>
				<label>' . $this->l('Status:') . ' </label>
				<div class="margin-form">
					<input type="radio" name="active" id="active_on" value="1" ' . ((!$obj->id OR $this->getFieldValue($obj, 'active')) ? 'checked="checked" ' : '') . '/>
					<label class="t" for="active_on"> <img src="../img/admin/enabled.gif" alt="" title="' . $this->l('Enabled') . '" /></label>
					<input type="radio" name="active" id="active_off" value="0" ' . ((!$this->getFieldValue($obj, 'active') AND $obj->id) ? 'checked="checked" ' : '') . '/>
					<label class="t" for="active_off"> <img src="../img/admin/disabled.gif" alt="" title="' . $this->l('Disabled') . '" /></label>
					<p>' . $this->l('Enabled or disabled') . '</p>
				</div>
				<label>' . $this->l('Contains districts:') . ' </label>
				<div class="margin-form">
					<input type="radio" name="contains_districts" id="contains_districts_on" value="1" ' . ((!$obj->id OR $this->getFieldValue($obj, 'contains_districts')) ? 'checked="checked" ' : '') . '/>
					<label class="t" for="contains_districts_on"> <img src="../img/admin/enabled.gif" alt="" title="" />' . $this->l('Yes') . '</label>
					<input type="radio" name="contains_districts" id="contains_districts_off" value="0" ' . ((!$this->getFieldValue($obj, 'contains_districts') AND $obj->id) ? 'checked="checked" ' : '') . '/>
					<label class="t" for="contains_districts_off"> <img src="../img/admin/disabled.gif" alt="" title="" />' . $this->l('No') . '</label>
				</div>
				<div class="margin-form">
					<input type="submit" value="' . $this->l('   Save   ') . '" name="submitAdd' . $this->table . '" class="button" />
				</div>
				<div class="small"><sup>*</sup> ' . $this->l('Required field') . '</div>
			</fieldset>
		</form>
		<script type="text/javascript">disableZipFormat();</script>';
    }
}

